package cc.rengu.oltp.service.common.dao.impl;

import cc.rengu.jradp.mods.xml.XmlTree;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.dao.SecPlanInfoMapper;
import cc.rengu.oltp.service.common.entity.SecPlanInfo;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 安全计划表操作接口实现类
 * Created by 王成 on 2020/2/10
 */
public class SecPlanInfoMapperImpl implements SecPlanInfoMapper {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppParamConstant.OLTP_POOL_NAME);

    public SecPlanInfoMapperImpl() throws Exception {
        if (XmlTree.hasThreadTree()) {
            XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
            String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
            if (null != corporation && !corporation.isEmpty()) {
                String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
                dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
            }
        }
    }

    @Override
    public int insertSecPlanInfo(SecPlanInfo secPlanInfo) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertSecPlanInfo");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, secPlanInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记安全计划信息失败,instId:<{}>,secPlanId:<{}>,secPlanType:<{}>,iReturnCode:<{}>", secPlanInfo.getInstId(), secPlanInfo.getSecPlanId(), secPlanInfo.getSecPlanType(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateSecPlanInfoByPrimaryKey(SecPlanInfo secPlanInfo) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateSecPlanInfoByPrimaryKey");
        secPlanInfo.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, secPlanInfo, null);
        if (0 != iReturnCode) {
            rglog.error("根据主键更新安全计划信息失败,instId:<{}>,secPlanId:<{}>,secPlanType:<{}>,iReturnCode:<{}>", secPlanInfo.getInstId(), secPlanInfo.getSecPlanId(), secPlanInfo.getSecPlanType(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public SecPlanInfo selectSecPlanInfoByPrimaryKey(String instId, String secPlanId, String secPlanType) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectSecPlanInfoByPrimaryKey");
        SecPlanInfo secPlanInfo = new SecPlanInfo();
        secPlanInfo.setInstId(instId);
        secPlanInfo.setSecPlanId(secPlanId);
        secPlanInfo.setSecPlanType(secPlanType);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, secPlanInfo, secPlanInfo);
        if (0 != iReturnCode) {
            rglog.warn("根据主键查询安全计划信息失败,instId:<{}>,secPlanId:<{}>,secPlanType:<{}>,iReturnCode:<{}>", instId, secPlanId, secPlanType, iReturnCode);
            return null;
        }
        return secPlanInfo;
    }

    @Override
    public List<SecPlanInfo> selectSecPlanInfoBySecPlanId(String instId, String secPlanId) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectSecPlanInfoBySecPlanId");
        SecPlanInfo secPlanInfo = new SecPlanInfo();
        secPlanInfo.setInstId(instId);
        secPlanInfo.setSecPlanId(secPlanId);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, secPlanInfo, SecPlanInfo.class);
        if (objectList.isEmpty()) {
            rglog.warn("根据法人行标识<{}>及安全计划标识<{}>获取所有安全计划信息失败!", instId, secPlanId);
            return null;
        }
        return objectList.stream().map(item -> (SecPlanInfo) (item)).collect(Collectors.toList());
    }
}
